﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeneralDFS.DataEntity.Nodes;
using ProtoBuf;

namespace GeneralDFS.DataEntity.Config
{
    [ProtoContract]
    [Serializable]
    public class DFSConfig
    {
        [ProtoMember(1, IsRequired = true)]
        public List<NameNodeConfig> NameNodeConfigs { get; set; }

        [ProtoMember(2, IsRequired = true)]
        public List<DataNodeConfig> DataNodeConfigs { get; set; }

        [ProtoMember(3, IsRequired = true)]
        public DetailConfig DetailConfig { get; set; }

        public CurrentSettings CurrentSettings { get; set; }

        [ProtoMember(4)]
        public long TimeCheckPoint { get; set; }

        public NameNodeConfig GetNameNodeConfigByServerName(String serverName)
        {
            return NameNodeConfigs.FirstOrDefault(nameNodeConfig => nameNodeConfig.ServerName == serverName);
        }

        public List<NameNodeInfo> NameNodeInfos()
        {
            var nameNodeInfos = new List<NameNodeInfo>();
                foreach (var nameNodeConfig in NameNodeConfigs)
                {
                    nameNodeInfos.Add(new NameNodeInfo { Ip = nameNodeConfig.Ip, RpcPort = nameNodeConfig.RpcPort, ServerName = nameNodeConfig.ServerName });
                }
                nameNodeInfos.Sort();
                return nameNodeInfos;
        }
    }
}